package com.ogawa.a7517.fragment;

import android.graphics.Path;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.reslib.utils.ToastUtils;
import com.ogawa.a7517.Constant;
import com.ogawa.a7517.DataManager;
import com.ogawa.a7517.R;
import com.ogawa.a7517.activity.MainActivity7517;
import com.ogawa.a7517.adapter.MainAdapter;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;
import com.ogawa.a7517.bean.ProgramBean;
import com.ogawa.a7517.mqtt.MqttManager;
import com.ogawa.a7517.utils.ViewUtil;
import com.ogawa.a7517.widget.HalfCircleView;
import com.ogawa.a7517.widget.pathlayoutmanager.PathLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class MainFragment extends BaseFragment {
    private int currentPos;
    private Group groupRunning;
    private ImageView ivRunning;
    private int running;
    private MainAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    public void initView(View view) {
        Bundle arguments = getArguments();
        boolean reset = false;
        if (arguments != null) {
            reset = arguments.getBoolean("reset");
        }
        HalfCircleView halfCircleView = view.findViewById(R.id.hc_view);
        TextView tvProgram = view.findViewById(R.id.tv_program);
        ivRunning = view.findViewById(R.id.iv_running_logo);
        groupRunning = view.findViewById(R.id.group_running);
        view.findViewById(R.id.iv_running_logo).setOnClickListener(view1 -> judgeToWhere(false));
        halfCircleView.post(() -> {
            Path path = halfCircleView.getPath();
            PathLayoutManager pathLayoutManager;
            pathLayoutManager = new PathLayoutManager(path, ViewUtil.dip2px(260, activity7517));
            pathLayoutManager.setAutoSelect(true);
            pathLayoutManager.setItemDirectionFixed(true);
            pathLayoutManager.setFlingEnable(true);
            pathLayoutManager.setScrollMode(PathLayoutManager.SCROLL_MODE_LOOP);
            pathLayoutManager.setItemScaleRatio(0.7f, 0, 1, 0.5f, 0.7f, 1);
            RecyclerView rlvMain = view.findViewById(R.id.rlv_main);
            rlvMain.setLayoutManager(pathLayoutManager);
            List<ProgramBean> programBeans = new ArrayList<>();
            programBeans.add(new ProgramBean(R.drawable.selector_home_shangban, getString(R.string.program_sbz), 1));
            programBeans.add(new ProgramBean(R.drawable.btn_aoto_n, getString(R.string.auto_program), 0));
            programBeans.add(new ProgramBean(R.drawable.btn_gaoji_n, getString(R.string.program_gjam), 0));
            programBeans.add(new ProgramBean(R.drawable.selector_home_nvwang, getString(R.string.program_nsmt), 9));
            programBeans.add(new ProgramBean(R.drawable.selector_home_taishi, getString(R.string.program_tsls), 16));
            programBeans.add(new ProgramBean(R.drawable.selector_home_laonian, getString(R.string.program_lnys), 13));
            programBeans.add(new ProgramBean(R.drawable.btn_quyu_n, getString(R.string.program_qyam), 0));
            programBeans.add(new ProgramBean(R.drawable.btn_collection_n, getString(R.string.collection), 0));
            pathLayoutManager.setOnItemSelectedListener(position -> {
                currentPos = position;
                tvProgram.setText(programBeans.get(position).getName());
            });
            adapter = new MainAdapter(programBeans);
            rlvMain.setAdapter(adapter);
            adapter.setOnItemClickListener((adapter1, view1, position) -> {
                if (currentPos != position) {
                    pathLayoutManager.smoothScrollToPosition(position);
                } else {
                    if (!activity7517.isSnExist()) {
                        return;
                    }
                    if (position == 1) {
                        activity7517.startToFragmentAddToStack(new AutoProgramFragment());
                    } else if (position == 7) {
                        activity7517.startToFragmentAddToStack(new CollectFragment());
                    } else if (position == 6) {
                        activity7517.startToFragmentAddToStack(new AreaMassageFragment());
                    } else if (position == 2) {
                        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
                        if (stateBean != null) {
                            judgeToWhere(true);
                        } else {
                            activity7517.startToFragmentAddToStack(new SeniorMassageFragment());
                        }
                    } else {
                        if (view1.isSelected()) {
                            judgeToWhere(false);
                        } else {
                            activity7517.publish(Constant.AUTO_PROGRAM, String.valueOf(programBeans.get(position).getModel()));
                        }
                    }
                }
            });
            pathLayoutManager.scrollToPosition(0);
            tvProgram.setText(programBeans.get(0).getName());
        });
        if (!reset) {
            activity7517.getMqttAccount();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (DataManager.getInst().isSnChange()) {
            MqttManager.getInstance().disconnect();
            activity7517.getMqttAccount();
            DataManager.getInst().setSnChange(false);
        } else {
            deviceStateChange();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle("");
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        if (!isInit) {
            return;
        }
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        boolean seniorMassage = MassageArmchair.getInstance().isSeniorMassage();
        if (stateBean != null) {
            if (stateBean.getFunctions().getAutoProgram() != null) {
                if (seniorMassage) {
                    groupRunning.setVisibility(View.VISIBLE);
                    ivRunning.setImageResource(R.drawable.btn_gaoji_s);
                }
                int auto = Integer.parseInt(stateBean.getFunctions().getAutoProgram().getModelValue());
                if (auto == 0) {
                    running = 0;
                    if (adapter != null) {
                        adapter.setRunning(0);
                    }
                    if (!seniorMassage) {
                        groupRunning.setVisibility(View.GONE);
                    }
                } else if (running != auto) {
                    running = auto;
                    groupRunning.setVisibility(View.VISIBLE);
                    if (adapter != null) {
                        adapter.setRunning(auto);
                    }
                    if (auto > 16) {
                        ivRunning.setImageResource(R.drawable.btn_quyu_s);
                    } else {
                        for (ProgramBean programBean : DataManager.getInst().getProgramBeans()) {
                            if (programBean.getModel() == auto) {
                                ivRunning.setImageResource(programBean.getImgRes());
                                ivRunning.setSelected(true);
                                break;
                            }
                        }
                    }
                }
            }
        } else {
            running = 0;
            if (adapter != null) {
                adapter.setRunning(0);
            }
            groupRunning.setVisibility(View.GONE);
        }
        if (adapter == null) {
            running = 0;
        }
    }
}
